import { ChallengeDetialOneEndPage } from './../challenge-detial/challenge-detial-one-end/challenge-detial-one-end';
import { MessagePage } from './../message/message';
import { NoticeService } from './../../providers/notice-service';
import { Api } from './../../providers/api-service';
import { UrlService } from './../../providers/url-service';
import { Component } from '@angular/core';
import { NavController, ModalController, App } from 'ionic-angular';
import { BasicDataPage } from '../basic-data/basic-data';
import { YueliaoPage } from '../yueliao/yueliao';
import { MyChallengePage } from './my-challenge/my-challenge';
import { MyRankingPage } from './my-ranking/my-ranking';
import { ChallengeDetialOneStartPage } from '../challenge-detial/challenge-detial-one-start/challenge-detial-one-start';
import { ChallengeDetialTwoStartPage } from '../challenge-detial/challenge-detial-two-start/challenge-detial-two-start';
import { ChallengeDetialThreeStartPage } from '../challenge-detial/challenge-detial-three-start/challenge-detial-three-start';
import { MyJPushService } from '../../providers/plugin-service/jPush-service';
import { StudyListPage } from '../haokur/study/study-list/study-list';
import { DiscoverSearchPage } from '../about/discover-search/discover-search';

@Component({
	selector: 'page-home',
	templateUrl: 'home.html'
})
export class HomePage {
	weiliao;
	len;
	recomlist = []; //推荐列表
	CityList = [];//同城推荐
	SesionBtnlist = []; //四大图标
	imgList = []; //轮播图
	pageSize = 10; //每页大小
	page = 1;
	doRefreshData = [
		{
			forbidFinite: true,
			type: true,//刷新类型
			refresher: null
		},
		{
			forbidFinite: true,
			type: true,//刷新类型
			refresher: null
		}
	];

	constructor(
		public navCtrl: NavController,
		public app: App,
		public modeCtrl: ModalController,
		public urlService: UrlService,
		public api: Api,
		public nitServ: NoticeService,
		public myJPushService: MyJPushService
	) {
		let PushKey = api.getUserinfo("PushKey");
		if (PushKey) {
			myJPushService.setAlias(PushKey);
		}

		this.weiliao = {
			txt: '约聊',
			css: { 'weiliao-txt': true }
		};

		this.doinite();
	}

	ionViewDidLoad() {
		//进入页面时自动播放  

	}
	ionViewDidLeave() {
		//页面离开时停止自动播放  
		// this.slides.stopAutoplay();
	}
	/**
	 * 四个按钮
	 */
	doinite() {
		this.SesionBtnlist = [
			{
				name: "btn_1",
				txt: '我的挑战',
				src: 'assets/imgs/img_home/Oval@3x.svg'
			},
			{
				name: "btn_2",
				txt: '战友排名',
				src: 'assets/imgs/img_home/yuezhan.png'
			},
			{
				name: "btn_3",
				txt: '奋力学习',
				src: 'assets/imgs/img_home/yuezhan.png'
			},
			{
				name: "btn_4",
				txt: '找战友',
				src: 'assets/imgs/img_home/yuezhan.png'
			}
		];
		this.getImgUrlArray();
		this.getChallengeList((this.page = 1));
		this.getChallengeCityList(this.CityListPage);
		this.settingBtn();
	}


	/**
   * 约聊按钮设置
   */
	settingBtn() {
		let Interval = false;
		setInterval(() => {
			if (Interval) {
				delete this.weiliao;
				this.weiliao = {
					txt: '约聊',
					css: { 'weiliao-txt': true }
				};
				Interval = false;
			} else {
				delete this.weiliao;
				this.weiliao = {
					txt: null,
					css: { 'weiliao-img': true }
				};
				Interval = true;
			}
		}, 5000);
	}
	/**
   * 点击顶部的按钮
   */
	goTalkAbout(index) {
		let page = null;
		if (index == 1) {
			page = YueliaoPage;
		} else {
			page = MessagePage;
		}
		this.navCtrl.push(page);
	}

	/**
   * 搜索页跳转
   */
	SearchClick() {
		//this.navCtrl.push(SearchPage);
		this.navCtrl.push(DiscoverSearchPage);
	}


	/**
   * 下拉刷新
   * @param refresher 
   */
	doRefresh(refresher) {
		this.page = 1;
		this.CityListPage = 1;
		this.doRefreshData[0].type = true;
		this.doRefreshData[1].type = true;
		this.doRefreshData[0].forbidFinite = true;
		this.doRefreshData[1].forbidFinite = true;
		this.doinite();
		setTimeout(() => {
			refresher.complete();
		}, 2000);
	}
	infiniteScroll = true;
	/**
   * 上啦加载更多
   * @param infiniteScroll 
   */
	doInfinite(infiniteScroll) {
		setTimeout(() => {
			if (this.isnav) {
				this.doRefreshData[0].type = false;
				this.getChallengeList(this.page);
			} else {
				this.doRefreshData[1].type = false;
				this.getChallengeCityList(this.CityListPage);
			}
			infiniteScroll.complete();
		}, 3000);
	}

	goMygoals() {
		this.app.getRootNav().push(BasicDataPage);
	}
	isnav = true;
	Chenked_tiaozhan(key) {
		this.infiniteScroll = this.doRefreshData[key].forbidFinite;
		if (key == 0) {
			this.isnav = true;
			if (this.doRefreshData[0].forbidFinite) {
			//	this.getChallengeList(this.page);
			}
		} else {
			this.isnav = false;
			if (this.doRefreshData[1].forbidFinite) {
				//this.getChallengeCityList(this.CityListPage);
			}
		}
	}

	/**
	 * 推荐挑战
	 * @param page 第几页
	 */
	getChallengeList(page) {
		let pageSize = 20;
		console.log("推荐挑战" + page + "页")
		this.urlService
			.GetNewRecommendDekaron(page, pageSize)
			.then((msg) => {
				if (msg.Code == 0) {
					let list = msg.Data;
					if (list.length > 0) {
						if (this.doRefreshData[0].type) { //初始化
							this.recomlist = list;
						} else {
							this.recomlist = this.recomlist.concat(list);
						}
					}
					if (list.length == pageSize) {
						this.page = this.page + 1;
					}
					if (list.length < pageSize) {
						this.doRefreshData[0].forbidFinite = false;
						this.infiniteScroll = this.doRefreshData[0].forbidFinite;
					}
				}
			})
			.catch((erre) => {
				//console.log(erre);
			});
	}
	CityListPage = 1;

	/**
	 * 同城挑战
	 */
	getChallengeCityList(CityListPage) {
		let city = "上海";
		this.api.postBody("V1/GetCityDekaron", { Cityid: city, PageIndex: CityListPage, PageSize: this.pageSize })
			.then(msg => {

				if (msg.Code == 0) {
					let list = msg.Data.Items;
					if (list.length > 0) {
						if (this.CityListPage == 1) { //初始化
							this.CityList = list;
						} else {
							this.CityList = this.CityList.concat(list);
						}

					}
					if (list.length == this.pageSize) {
						this.CityListPage = this.CityListPage + 1;
					}
					if (list.length < this.pageSize) {
						this.doRefreshData[1].forbidFinite = false;
						this.infiniteScroll = this.doRefreshData[1].forbidFinite;
					}
				}
			});
	}
	slidesList = [];
	/**
	 * 首页轮播图
	 */
	getImgUrlArray() {
		this.urlService.getHomeData()
			.then(msg => {
				this.len = msg.Data.length;
				this.slidesList = msg.Data;
			})
			.catch(error => {

			});
	}
	/**
	 * 挑战详情
	 * @param obj 
	 */
	openChangeDetial(obj, key) {
		let page;
		if (obj != null && obj != undefined) {
			if (obj.Pattern == 10) {
				if (obj.State == 10) {
					page = ChallengeDetialOneStartPage;
				} else {
					page = ChallengeDetialOneEndPage;
				}
				page = ChallengeDetialTwoStartPage;
			} else if (obj.Pattern == 20) {
				page = ChallengeDetialTwoStartPage;
			} else {
				page = ChallengeDetialThreeStartPage;
			}
			
			if (key == 0) {
				obj = obj.DekaronID;
			} else {
				obj = obj.ID;
			}
			this.navCtrl.push(page, { "ChallengeDetialID": obj });
		}
	}
	/**
	 * 挑战
	 * @param key 
	 */
	btnIndex(key) {
		let strPage;
		switch (key) {
			case "btn_1": //我的挑战
				strPage = MyChallengePage;
				break;
			case "btn_2"://战友排名
				strPage = MyRankingPage;
				break;
			case "btn_3": //奋力学习,学习列表
				strPage = StudyListPage;
				break;
			case "btn_4"://同城挑战
				strPage = "";
				break;
		}

		if (strPage != null && strPage != undefined && strPage != "") {
			this.navCtrl.push(strPage);
		}
	}

}
